{extend name="layout/base"}

{block name="content"}

<form class="layui-form">
    <div class="layui-container">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="hidden" name="menu_id" value="{$menu.menu_id}"/>
                <input type="text" class="layui-input" name="menu_name" placeholder="请输入名称" lay-verify="required" value="{$menu.menu_name}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">父级菜单</label>
            <div class="layui-input-block">
                <div>
                    <select name="menu_pid" lay-verify="required">
                        <option value="0">顶级菜单</option>
                        {foreach $menus as $key=>$value }
                        <option value="{$value.menu_id}" {if $menu.menu_pid == $value.menu_id }selected = "selected"{/if}>{$value.html}{$value.menu_name}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">路径</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="menu_path" placeholder="请输入路径" value="{$menu.menu_path}">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <div>
                    <select name="roleId" xm-select="roleId">
                        {foreach $roles as $key=>$value }
                        {if in_array($value.role_id, $myRoles)}
                        <option value="{$value.role_id}" selected = "selected">{$value.role_name}</option>
                        {else /}
                        <option value="{$value.role_id}">{$value.role_name}</option>
                        {/if}
                        {/foreach}
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-inline" style="margin-left: 100px">
                <button class="layui-btn  layui-text-center" lay-submit lay-filter="submit">修改</button>
            </div>
        </div>
    </div>
</form>

<script>
    layui.use(['jquery', 'form', 'layer', 'formSelects'], function ($, form, layer, formSelects) {
        formSelects.render('roleId');
        form.on('submit(submit)', function (obj) {
            var roleId = formSelects.value('roleId', 'valStr');
            obj.field.roleId = roleId;

            $.ajax({
                type: "post",
                url: "{:url('menu/edit')}",
                dataType: 'json',
                data: obj.field,
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 5});
                        parent.location.href = "{:url('menu/lists')}"
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function () {
                    layer.msg('请求错误', {icon: 6});
                }
            });

            return false;
        })
    });

</script>
{/block}